<template>
    <rx-dialog @ok="handOk" @cancel="closeWindow">
        <rx-layout style="padding: 10px;">
            <template #middle>
                <a-form :layout="layout">
                    <a-form-item :label="i18n('nodesExecutors.destNodeId','跳转节点')">
                        <a-select v-model="destNodeId" :getPopupContainer="(p)=>p.parentNode" @change="nodeChange">
                            <a-select-option v-for="executor in nodeExecutors" :value="executor.nodeId">
                                {{ executor.nodeName }}
                            </a-select-option>
                        </a-select>

                    </a-form-item>
                    <a-form-item :label="i18n('nodesExecutors.executors','执行人')"
                                 v-if="nodeExecutor.nodeType=='UserTask' ">
                        <div
                            v-if="nodeExecutor.users.length>0 ||  (nodeExecutor.users.length==0 && (!nodeExecutor.groups || nodeExecutor.groups.length==0)) "
                            class="bpm-task-executors-tag">
                            <bpm-executors v-model="nodeExecutor.users" :filter="nodeExecutor.userFilter"
                                           :single="nodeExecutor.single" :readonly="false"></bpm-executors>
                        </div>
                        <!-- 部门  -->
                        <div v-else class="bpm-task-executors-tag">
                            <rx-group-component v-model="nodeExecutor.groups" :filter="nodeExecutor.groupFilter"
                                                :single="nodeExecutor.single" :readonly="false"></rx-group-component>
                        </div>
                    </a-form-item>
                    <a-form-item :label="i18n('taskCheck.opinion','意见')">
                        <a-textarea :placeholder="i18n('nodesExecutors.opinionPlaceholder','请填写意见')"
                                    v-model:value="opinion" :autoSize="{ minRows: 2, maxRows: 6 }"></a-textarea>
                    </a-form-item>
                </a-form>
            </template>
        </rx-layout>
    </rx-dialog>
</template>

<script>
import BpmDefApi from "@/common/api/bpm/bpmDef"
import BpmInstApi from "@/common/api/bpm/bpmInst";
import BpmTaskApi from "@/common/api/bpm/bpmTask";
import {Util, RxDialog, RxLayout} from "@lowcode/jpaas-base-lib";
import BpmExecutors from "@/views/modules/bpm/components/BpmExecutors.vue";


export default {
    name: "NodesExecutors",
    props: ["layerid", "destroy", "bpmTask", "formData", "taskConfig", "processConfig"],
    components: {
        RxDialog,
        RxLayout,
        BpmExecutors
    },
    data() {
        return {
            checkType: "AGREE",
            nodeExecutors: [],
            nodeExecutor: '',
            opinion: "",
            destNodeId: "",
            selectExecutor: {},
            executorKey: Util.randomId(),
            labelCol: {span: 6},
            wrapperCol: {span: 18},
            layout: "vertical"
        }
    },
    created() {
        this.getJumpNodes();
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmTask", key);
        },
        getJumpNodes() {
            let nodeId = this.bpmTask.key;
            BpmDefApi.getJumpNodes(this.bpmTask.actDefId).then(res => {
                let len = res.length - 1;
                for (let i = len; i >= 0; i--) {
                    let node = res[i];
                    if (node.nodeId == nodeId) {
                        res.splice(i, 1);
                    }
                }
                this.nodeExecutors = res;
                if (res && res.length > 0) {
                    this.destNodeId = res[0].nodeId;
                    this.nodeChange(this.destNodeId);
                }
            })


        },
        taskNodeJump(vm) {
            let self = this;
            let cmd = {
                taskId: this.bpmTask.taskId,
                destNodeId: this.destNodeId,
                checkType: this.checkType,
                formJson: JSON.stringify(self.formData),
                nodeExecutors: self.selectExecutor,
                opinion: this.opinion,
                msgTypes: "",
                copyUserAccounts: "",
                systemHand: true,
                opFiles: "[]",
                relInsts: "[]"
            }
            BpmTaskApi.checkComplete(cmd).then(res => {
                vm.loading = false;
                if (res.success) {
                    Util.closeWindow(self, 'ok');
                }
            }).cath(res => {
                vm.loading = false;
            });
        },
        handOk(vm) {
            if (!this.destNodeId) {
                this.$message.warning(this.i18n("msa.jumpNodeTips", "请选择跳转节点！"));
                vm.loading = false;
                return;
            }
            let self = this;

            let obj = {};
            this.convertUsers(this.nodeExecutor);
            obj[this.destNodeId] = this.nodeExecutor.executors;
            this.selectExecutor = obj;

            //是否为结束节点
            if (this.nodeExecutor.nodeType == "EndEvent") {
                this.$confirm({
                    title: this.$ti18('rxMsa.actionTips', '操作提示'),
                    zIndex: 20000,
                    content: this.i18n("msa.endEventTips", '当前为结束节点，您确定需要结束当前流程吗？'),
                    okText: this.$ti18('rxForm.okBtn', '确定'),
                    cancelText: this.$ti18('rxForm.cancelBtn', '取消'),
                    onOk() {
                        //跳转到目标节点
                        self.taskNodeJump(vm);
                    },
                    onCancel() {
                        vm.loading = false;
                    }
                })
            } else {
                //跳转到目标节点
                this.taskNodeJump(vm);
            }
        },
        closeWindow() {
            Util.closeWindow(this, 'cancel');
        },
        nodeChange(value) {
            if (!value) {
                return;
            }

            let self_ = this;
            let nodes = this.nodeExecutors.filter(item => {
                return item.nodeId == value;
            });
            let node = nodes[0];
            //结束节点直接返回。
            if (node.nodeType == "EndEvent") {
                return;
            }
            let instId = this.bpmTask.instId;

            BpmInstApi.getNodeExecutors(instId, value, self_.formData).then(res => {
                self_.executorKey = Util.randomId();
                self_.convertToLocalExecutors(res);
                self_.nodeExecutor = res;
            })

        },
        convertToLocalExecutors(o) {
            var users = [];
            var groups = [];
            var executors = o.executors;
            for (var i = 0; i < executors.length; i++) {
                var executor = executors[i];
                if (executor.type == 'user') {
                    users.push(executor);
                } else {
                    groups.push(executor);
                }
            }
            o.users = users;
            o.groups = groups;
        },
        convertUsers(o) {
            delete o.executors;
            var executors = [];

            if (o.users) {
                for (var i = 0; i < o.users.length; i++) {
                    var user = o.users[i];
                    user.type = "user";
                    executors.push(user);
                }
            }

            if (o.groups) {
                for (var i = 0; i < o.groups.length; i++) {
                    var group = o.groups[i];
                    group.type = "group";
                    executors.push(group);
                }
            }

            o.executors = executors;
        }
    },
    watch: {}
}
</script>

<style scoped>
.ant-form-item {
    margin-bottom: 2px !important;
}
</style>
